using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataBase.Core;

namespace MeetFuture.MRC
{
	public partial class clsRackInfo
	{
        private string _ConnString = ApplicationDomain.Instance.GetAppConfig(ApplicationGlobal.key_erackdb);

        private const string key_Name = "RACKNAME=";
        private const string key_RackPosition = "RACKPOS=";
        private const string key_split = ",";

        private string _Name = string.Empty;
        private int _RackWidth = -1;
        private int _RackHeight = -1;
        private Dictionary<string, clsRackPosition> _RackPositions = new Dictionary<string, clsRackPosition>();

        public clsRackInfo(string RackName)
        {
            this._Name = RackName;
            this._RackPositions = new Dictionary<string, clsRackPosition>();            
        }
        public string Name
        {
            get { return this._Name; }
        }
        public int RackWidth
        {
            set { this._RackWidth = value; }
            get { return this._RackWidth; }
        }
        public int RackHeight
        {
            set { this._RackHeight = value; }
            get { return this._RackHeight; }
        }
        public Dictionary<string, clsRackPosition> RackPositions
        {
            get {return this._RackPositions;}
        }
        public void Add(clsRackPosition RackPosition)
        {
            try
            {
                clsRackPosition tmpPos = null;
                this._RackPositions.TryGetValue(RackPosition.PositionID, out tmpPos);
                if (tmpPos == null)
                {
                    this._RackPositions.Add(RackPosition.PositionID, RackPosition);
                }
                else
                {
                    this._RackPositions[RackPosition.PositionID] = RackPosition;
                }
            }
            catch (System.Exception ex)
            {	
            }            
        }
        public void Update(clsRackPosition RackPosition)
        {
            try
            {
                this.UpdateInfo();
                clsRackPosition rackPos = null;
                this._RackPositions.TryGetValue(RackPosition.PositionID, out rackPos);
                if (rackPos != null)
                {
                    rackPos.LotID = RackPosition.LotID;
                    rackPos.UpdateTime = RackPosition.UpdateTime;
                    rackPos.PositionType = RackPosition.PositionType;
                }
            }
            catch (System.Exception ex)
            {	
            }
        }        
        public string FindEmptyPosition(clsRackPosition.RackPositionType PosType)
        {
            string result = string.Empty;
            try
            {
                this.UpdateInfo();
                foreach (clsRackPosition rackPos in this._RackPositions.Values)
                {
                    if (rackPos.PositionType == PosType)
                    {
                        if (string.IsNullOrWhiteSpace(rackPos.LotID))
                        {
                            result = rackPos.PositionID;
                            break;
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }        
        public string FindPositionByLot(string LotId)
        {
            string result = string.Empty;
            try
            {
                this.UpdateInfo();
            	foreach (clsRackPosition rackPos in this._RackPositions.Values)
            	{
            	    if (rackPos.LotID == LotId)
            	    {
                        result = rackPos.PositionID;
                        break;
            	    }
            	}
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }
        public string FindLotByPosition(string PostionID)
        {
            string result = string.Empty;
            try
            {
                this.UpdateInfo();
                clsRackPosition rackPos = null;
                this._RackPositions.TryGetValue(PostionID, out rackPos);
                if (rackPos != null)
                {
                    result = rackPos.LotID;
                }
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }
        public clsRackPosition FindclsPositionByPositonID(string PositionID)
        {
            clsRackPosition result = null;
            try
            {
                this.UpdateInfo();
                this._RackPositions.TryGetValue(PositionID, out result);                
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }
        public clsRackPosition FindclsPositionByLot(string LotId)
        {
            clsRackPosition result = null;
            try
            {
                this.UpdateInfo();
                foreach (clsRackPosition rackPos in this._RackPositions.Values)
                {
                    if (rackPos.LotID == LotId)
                    {
                        result = rackPos;                        
                        break;
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }
        public clsRackPosition FindclsPositionByLot(string LotId, clsRackPosition.RackPositionType PosType)
        {
            clsRackPosition result = null;
            try
            {
                this.UpdateInfo();
            	foreach (clsRackPosition rackPos in this._RackPositions.Values)
            	{
            	    if (rackPos.LotID == LotId)
            	    {
                        if (rackPos.PositionType == PosType)
                        {
                            result = rackPos;
                        } 
                        else
                        {
                            result = null;
                        }                        
                        break;
            	    }
            	}
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }
        public clsRackPosition FindclsPositionByShowID(string ShowID)
        {
            clsRackPosition result = null;
            try
            {
                this.UpdateInfo();
                foreach (clsRackPosition rackpos in this._RackPositions.Values)
                {
                    if (rackpos.ShowID == ShowID)
                    {
                        result = rackpos;
                        break;
                    }
                }
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }
        public void UpdateInfo()
        {
            try
            {
                this._RackPositions = new Dictionary<string, clsRackPosition>();
                clsRackInfo rackinfo = ApplicationDomain.Instance.DBHelper_ERack.GetRack(this._Name);
                if (rackinfo != null)
                {
                    this._Name = rackinfo.Name;
                    this._RackHeight = rackinfo.RackHeight;
                    this._RackWidth = rackinfo.RackWidth;
                    this._RackPositions = rackinfo.RackPositions;
                }
            }
            catch (System.Exception ex)
            {
            }
        }
        public string ToObjectString(clsRackPosition.RackPositionType PosType)
        {
            string result = string.Empty;
            try
            {
                result += "[" + key_Name + this._Name.ToUpper() + key_split;
                result += key_RackPosition + "[";
                foreach (clsRackPosition rackPos in this._RackPositions.Values)
                {
                    if (rackPos.PositionType == PosType)
                    {
                    	result += rackPos.ToObjectString();
                    }                                     
                }
                result += "]";
                result += "]";
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }
	}
}
